﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SERVICES.Services
{
    public static class CatalogService
    {
        /// <summary>
        /// Get all catalog in database
        /// </summary>
        /// <returns></returns>
        public static List<CATALOG> GetAllCatalogs()
        {
            try
            {
                using (var db = new WMILKEntities2())
                {
                   return db.CATALOGS.ToList();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Không thể lấy Catalogs" + ex.Message);
            }
        }

        /// <summary>
        /// return catalogs list with the most number of products
        /// </summary>
        /// <param name="numberOfCatalogs">number of catalogs will be retured</param>
        /// <returns></returns>
        public static List<CATALOG> GetTopCatalogs(int numberOfCatalogs)
        {
            using (var db = new WMILKEntities2())
            {
                return db.CATALOGS.OrderByDescending(x => x.PRODUCTS.Count).Take(numberOfCatalogs).ToList();
            }
        }

        public static List<CATALOG> GetCatalogsBySubCatalogs(int subCatalogsId)
        {
            using (var db= new WMILKEntities2())
            {
                return db.CATALOG_DETAILS.Where(x => x.ID_SUBCATALOG == subCatalogsId).Select(x=>x.CATALOG).ToList();
            }
        }
    }
}
